﻿#if UNITY_EDITOR
using System;
using System.Collections.Generic;

namespace JProfiler
{
    public class JItemData : IComparable<JItemData>
    {
        public string name;
        public int type;
        public int totalCount;
        public long totalCost;
        public long costAve;
        public long costMin;
        public long costMax;
        public long totalMem;
        public long memAve;
        public long memMin;
        public long memMax;
        public JProfileData data;

        public JItemData(string name, int type, long cost, long mem)
        {
            this.name = name;
            this.type = type;
            this.totalCount = 1;
            this.totalCost = this.costAve = this.costMax = this.costMin = cost;
            this.totalMem = this.memAve = this.memMax = this.memMin = mem;
        }

        public int CompareTo(JItemData other)
        {
            return name.CompareTo(other.name);
        }

        public float countPerSec
        { 
            get
            {
                int cps = (int)(this.totalCount * 1.0f / (data.EndTime - data.StartTime).TotalMilliseconds * 1000 * 100);
                return cps * 1.0f / 100;
            }
        }

        public float countPerMin
        {
            get
            {
                int cps = (int)(this.totalCount * 1.0f / (data.EndTime - data.StartTime).TotalMilliseconds * 1000 * 60 * 100);
                return cps * 1.0f / 100;
            }
        }

        public string GetNameByColumn(int column)
        {
            string ret = "";
            switch((MyColumns)column)
            {
                case MyColumns.Name:
                    ret = this.name;
                    break;
                case MyColumns.Type:
                    ret = this.type == 0 ? "C#" : "Lua";
                    break;
                case MyColumns.TotalCount:
                    ret = this.totalCount.ToString();
                    break;
                case MyColumns.CountPerSec:
                    ret = countPerSec.ToString();
                    break;
                case MyColumns.CountPerMin:
                    ret = countPerMin.ToString();
                    break;
                case MyColumns.TotalCost:
                    ret = this.totalCost.ToString();
                    break;
                case MyColumns.CostAve:
                    ret = this.costAve.ToString();
                    break;
                case MyColumns.CostMin:
                    ret = this.costMin.ToString();
                    break;
                case MyColumns.CostMax:
                    ret = this.costMax.ToString();
                    break;
                case MyColumns.TotalMem:
                    ret = this.totalMem.ToString();
                    break;
                case MyColumns.MemAve:
                    ret = this.memAve.ToString();
                    break;
                case MyColumns.MemMin:
                    ret = this.memAve.ToString();
                    break;
                case MyColumns.MemMax:
                    ret = this.memMax.ToString();
                    break;
            }
            return ret;
        }
        public string GetTipByColumn(int column)
        {
            return GetNameByColumn(column);
        }
    }
    public class JProfileData
    {
        private int m_Version = 0;
        private DateTime m_StartTime;
        private DateTime m_EndTime;
        private Dictionary<string, JItemData> m_data = new Dictionary<string, JItemData>();

        public Dictionary<string, JItemData> ItemDatas
        {
            get
            {
                return m_data;
            }
        }

        public int GetCount()
        {
            return m_data.Count;
        }

        public DateTime StartTime
        {
            get
            {
                return m_StartTime;
            }
            set
            {
                m_StartTime = value;
            }
        }

        public DateTime EndTime
        {
            get
            {
                return m_EndTime <= m_StartTime ? DateTime.Now : m_EndTime;
            }
            set
            {
                m_EndTime = value;
            }
        }

        public void Record(string key, int type, long cost, long mem)
        {
            if(!m_data.ContainsKey(key))
            {
                m_data[key] = new JItemData(key, type, cost, mem);
            }
            else
            {
                JItemData item = m_data[key];
                item.totalCount++;
                item.totalCost += cost;
                item.costAve = item.totalCost / item.totalCount;
                item.costMin = Math.Min(item.costMin, cost);
                item.costMax = Math.Max(item.costMax, cost);
                item.totalMem += mem;
                item.memAve = item.totalMem / item.totalCount;
                item.memMin = Math.Min(item.memMin, cost);
                item.memMax = Math.Max(item.memMax, cost);
            }
            m_data[key].data = this;
        }

    }
}
#endif